package com.uenpay.oem.ui.reserver;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.uenpay.im.utilities.Toasts;
import com.uenpay.im.weight.dialog.LoadDialog;
import com.uenpay.oem.MyApplication;
import com.uenpay.oem.R;
import com.uenpay.oem.common.AppConstant;
import com.uenpay.res.widget.dialog.WhiteBgDialog;
import com.uenpay.service.bean.BankCardInfo;
import com.uenpay.util.app.base.BaseActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by PS on 2017/6/19.
 */
public class AddnewcardActivity extends BaseActivity implements AddnewcardContract.View{

    private AddnewcardContract.Pesenter mPresenter;
    @BindView(R.id.tv_name)
    TextView tvPerson;
    @BindView(R.id.et_code)
    EditText etBankCardCode;
    private String name;
    private String num;
    private BankCardInfo info;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("添加银行卡");


            //点击下一步跳转
            findViewById(R.id.next_step).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    name = tvPerson.getText().toString().trim();
                    num = etBankCardCode.getText().toString().trim();
                    mPresenter.checkData(name, num);
                }
            });
        setPresenter(null);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_addcard;
    }

    @Override
    public void initView(View view) {
        name = MyApplication.getUserAuthInfo().getRealName();
        tvPerson.setText(name);
    }

    public void goNext() {
        Intent intent;
        if("1".equals(info.getCardType())){
             intent = new Intent(AddnewcardActivity.this, ReservedphoneActivity.class);
        } else {
            intent = new Intent(AddnewcardActivity.this, ReservedcodeActivity.class);
        }
        info.setUserName(name);
        intent.putExtra("bankInfo",info);
        startActivityForResult(intent, AppConstant.ADD_BANK_REQUEST_CODE);
    }
    @OnClick(R.id.iv_info)
    public void showInfo(){
        final WhiteBgDialog dialog = new WhiteBgDialog(this, R.layout.dialog_show_info);
        dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dissmiss();
            }
        });
    }
    @OnClick(R.id.iv_camera)
    public void takePhoto(){
        Intent intent = new Intent(this, CardCameraActivity.class);
        startActivityForResult(intent,AppConstant.RECOGNITED_CARD_REQUEST_CODE);
    }

    @Override
    public void setPresenter(Object presenter) {
        mPresenter = new AddnewcardPresenter(this);
    }

    @Override
    public void showToast(String msg) {
        Toasts.shortToast(this,msg);
    }

    @Override
    public void showLoading() {
        LoadDialog.show(this);
    }

    @Override
    public void dismissLoading() {
        LoadDialog.dismiss(this);
    }

    @Override
    public void setData(BankCardInfo info) {
        this.info = info;
        num = info.getCardNo();
        goNext();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
       //添加成功，finish
        if (requestCode == AppConstant.ADD_BANK_REQUEST_CODE && resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            finish();
        }
        if (requestCode == AppConstant.RECOGNITED_CARD_REQUEST_CODE && resultCode == RESULT_OK) {
            info = data.getParcelableExtra("bankInfo");
            etBankCardCode.setText(info.getCardNo());
        }
    }
}
